package com.intramirror.product.algorithm.impl;

import com.intramirror.product.algorithm.IAggregateAlgorithm;
import com.intramirror.product.entity.Product;
import com.intramirror.product.entity.Spu;
import com.intramirror.product.exception.BusinessException;
import com.intramirror.product.service.ISpuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created on 2018/2/26.
 * @author 123
 */
@Service("matchFromExists")
public class MatchFromExistsSpu implements IAggregateAlgorithm {
    private static final Logger LOGGER = LoggerFactory.getLogger(MatchFromExistsSpu.class);

    private ISpuService spuService;

    public MatchFromExistsSpu(ISpuService spuService) {
        this.spuService = spuService;
    }

    @Override
    public Spu aggregateWithPattern(Product product, String pattern) throws BusinessException {

        Spu spu = spuService.findExistsSpuByDesignerId(product.getDesignerId());
        if (spu != null) {
            LOGGER.debug("Find spu for product {}, designer id {} color code {}.", product.getProductId(), spu.getDesignerId(), spu.getColorCode());
            return spu;
        }
        throw new BusinessException("Could not aggregate product from exists spu");

    }
}
